import store from '@/store'
import { get, post } from '@/utils/request'

const cacheData = {}

const prefixUrl = '/api/adminPortal'
const tailUrl = 'getListByCon'

// 不进行分页
const NOT_PAGER = { is_paginate: 0 }

const mapDataCallBack = (res, keyName = 'name') => {
  return res.data.map((item) => ({ id: item.id, name: item[keyName] }))
}

/**
 * 缓存数据
 * @param {string} api 接口地址
 * @param {string} params 参数地址 用于区分缓存 ‘’
 * @param {string} key name标记
 */
const cacheAction = (api, params = {}, key = 'name') => {
  const cacheParams = JSON.stringify(params)
  return new Promise((resolve) => {
    if (cacheData[`${api}${cacheParams}`]) {
      resolve(cacheData[`${api}${cacheParams}`])
      return
    }
    get(api, { ...NOT_PAGER, ...params })
      .then((res) => mapDataCallBack(res, key)).then((data) => {
        cacheData[`${api}${cacheParams}`] = data
        resolve(cacheData[`${api}${cacheParams}`])
      })
  })
}

// 清空缓存
export const emptyCacheAction = () => {
  Object.keys(cacheData).forEach((key) => delete cacheData[key])
}
export function handleDeleteStudent(id) {
  return post('api/adminPortal/student/delete', {
    id
  })
}
// 登录
export function login(data) {
  return post('api/adminPortal/accountLogin/login', data)
}
// 用户修改密码
export function upDatPassWord() {
  return post('api/adminPortal/accountLogin/updatePassword')
}

// 获取个人信息
export const getUserInfo = () => get(`${prefixUrl}/self/getSelfInfo`)

// 修改密码
export const modifySelfPassword = (form) => post('api/adminPortal/accountLogin/updatePassword', form)

// 获取会计科目明细列表
export const getAccountingSubjectDetail = (accounting_subject_id) =>
  get(`${prefixUrl}/accountingSubjectDetail/${tailUrl}`, { ...NOT_PAGER, accounting_subject_id })
    .then(mapDataCallBack)

// 获取会计凭证类型
export const getAccountingVoucherType = () => get(`${prefixUrl}/accountingVoucherType/${tailUrl}`, NOT_PAGER)
  .then(mapDataCallBack)

// 获取帐套类型
export const getFinancialAccountSet = () => cacheAction(`${prefixUrl}/financialAccountSet/${tailUrl}`)

// 获取公司列表
export const getCompany = (type = '') => cacheAction(`${prefixUrl}/company/${tailUrl}`, { type }, 'company_name')

// 获取公司列表 根据 userid
export const getCompanyByUserId = () => get(`${prefixUrl}/company/${tailUrl}`, { ...NOT_PAGER, admin_id: store.state.user.userId })
  .then((res) => mapDataCallBack(res, 'company_name'))

// 获取城市信息 cityLevel 城市级别 parentId 上级城市id
export const getProvinceCity = (cityLevel = 1, parentId = '') =>
  cacheAction(`${prefixUrl}/provinceCity/${tailUrl}`, { cityLevel, parentId })

// 获取关联出租方
export const getLessor = () => get(`${prefixUrl}/lessor/${tailUrl}`, NOT_PAGER)
  .then((res) => mapDataCallBack(res, 'lessor_name'))

// 获取费用
export const getFeeType = (type, use) => cacheAction(`${prefixUrl}/feeType/${tailUrl}`, { type, use })

// 获取角色列表
export const getRoleList = () => cacheAction(`${prefixUrl}/role/${tailUrl}`)

// 根据公司获取部门信息
export const getDepartByCompanyId = (company_id) => cacheAction(`${prefixUrl}/companyDepartment/${tailUrl}`, { ...NOT_PAGER, company_id })
